//
//  ProfileTagsView.swift
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/2/2.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

import Foundation

class ProfileUserTagsView: UIView {

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (UIScreen.main.bounds.width - 240 - 280) / 2, height: 40)
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.sectionInset = .zero
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.registerCellClass(with: Cell.self)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        return collectionView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    private func setup() {
        addSubview(collectionView)
        collectionView.mas_makeConstraints { (make) in
            make?.edges.equalTo()(self)?.insets()(UIEdgeInsets(top: 25, left: 0, bottom: 100, right: 0))
        }
    }

    var titles: [String] = []


    class Cell: UICollectionViewCell {

        private var lastColor: CGColor?

        override var isHighlighted: Bool {
            set {
                if newValue {
                    guard var components = shapeLayer.fillColor?.components,
                        components.count >= 3 else { return }
                    components = components.map{ ($0 * 1.2) > 1 ? 1 : ($0 * 1.2) }
                    let color = UIColor(red: components[0], green: components[1], blue: components[2], alpha: components.count > 3 ? components[3] : 1)
                    shapeLayer.fillColor = color.cgColor
                    lastColor = color.cgColor
                } else if lastColor != nil {
                    shapeLayer.fillColor = lastColor
                }
            }
            get {
                return super.isHighlighted
            }
        }

        override init(frame: CGRect) {
            super.init(frame: frame)

            contentView.layer.addSublayer(shapeLayer)
            contentView.addSubview(titleLabel)
            contentView.addSubview(numberLabel)
            contentView.backgroundColor = .clear
            backgroundColor = .clear

            numberLabel.layer.cornerRadius = 7
            numberLabel.layer.borderColor = UIColor.white.cgColor
            numberLabel.layer.borderWidth = 2

            titleLabel.mas_makeConstraints { (make) in
                make?.left.equalTo()(contentView)?.offset()(14)
                make?.centerY.equalTo()(contentView)
            }

            numberLabel.mas_makeConstraints { (make) in
                make?.left.top().equalTo()(contentView)
                make?.width.height().equalTo()(14)
            }
        }

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

        override func layoutSubviews() {
            super.layoutSubviews()
            shapeLayer.frame = CGRect(x: 7, y: 7, width: width - 7, height: height - 14)
        }

        var title: String? {
            didSet {
                numberLabel.text = "\(arc4random_uniform(10))"
                titleLabel.text = "⭐️测试测试测试测试"
                titleLabel.sizeToFit()
                let width = titleLabel.width
                let rect = CGRect(x: 0, y: 0, width: width + 40, height: 24)
                let path = UIBezierPath(roundedRect: rect, cornerRadius: 4)
                let triangle = UIBezierPath()
                triangle.move(to: CGPoint(x: rect.maxX, y: 0))
                triangle.addLine(to: CGPoint(x: rect.maxX + 12, y: 12))
                triangle.addLine(to: CGPoint(x: rect.maxX, y: 24))
                path.append(triangle)
                shapeLayer.fillColor = UIColor.random().cgColor
                shapeLayer.path = path.cgPath
            }
        }

        let shapeLayer = CAShapeLayer()

        let numberLabel: UILabel = {
            let label = UILabel()
            label.textAlignment = .center
            label.numberOfLines = 1
            label.font = UIFont.systemMediumFont(withSize: 8)
            label.textColor = .white
            return label
        }()

        let starImageView: UIImageView = {
            let imageView = UIImageView()
//            imageView.image =
            return imageView
        }()

        let titleLabel: UILabel = {
            let label = UILabel()
            label.textAlignment = .left
            label.numberOfLines = 1
            label.font = UIFont.systemMediumFont(withSize: 10)
            label.textColor = .white
            return label
        }()
    }
}

extension ProfileUserTagsView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 90//titles.count
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: Cell = collectionView.dequeueReusableCell(for: indexPath)
        cell.title = "呵呵呵呵"
        return cell
    }
}
